package com.fr.data.impl;

import com.fr.base.StringUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.json.JSONObject;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.data.AbstractTableData;
import com.fr.data.Semantic;
import com.fr.data.TableData;
import com.fr.data.TableDataException;
import com.fr.report.adhoc.TransJSON;
import com.fr.report.script.Calculator;

public abstract class ADHOCTableData extends AbstractTableData
  implements TransJSON
{
  private Semantic semantic = null;
  private String description = null;
  protected TableData dt = null;

  public ADHOCTableData()
  {
  }

  public ADHOCTableData(String paramString, Semantic paramSemantic)
  {
    this.description = paramString;
    setSemantic(paramSemantic);
  }

  protected abstract TableData createTableData();

  public String getDescription()
  {
    return this.description;
  }

  public void setDescription(String paramString)
  {
    this.description = paramString;
  }

  public Semantic getSemantic()
  {
    return this.semantic;
  }

  public void setSemantic(Semantic paramSemantic)
  {
    this.semantic = paramSemantic;
  }

  public int getColumnCount()
    throws TableDataException
  {
    return createTableData().getColumnCount();
  }

  public String getColumnName(int paramInt)
    throws TableDataException
  {
    String str = createTableData().getColumnName(paramInt);
    if (getSemantic() != null)
    {
      Object localObject = getSemantic().getSemanticName(str, Calculator.createCalculator());
      if (localObject != null)
        return localObject.toString();
    }
    return str;
  }

  public int getRowCount()
    throws TableDataException
  {
    return createTableData().getRowCount();
  }

  public Object getValueAt(int paramInt1, int paramInt2)
    throws TableDataException
  {
    return createTableData().getValueAt(paramInt1, paramInt2);
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if (paramXMLableReader.isChildNode())
    {
      String str = paramXMLableReader.getTagName();
      if ("Description".equals(str))
      {
        setDescription(paramXMLableReader.getElementValue());
      }
      else if ("Semantic".equals(str))
      {
        this.semantic = new Semantic();
        paramXMLableReader.readXMLObject(this.semantic);
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    if (StringUtils.isNotEmpty(this.description))
      paramXMLPrintWriter.startTAG("Description").textNode(this.description).end();
    if (this.semantic != null)
    {
      paramXMLPrintWriter.startTAG("Semantic");
      this.semantic.writeXML(paramXMLPrintWriter);
      paramXMLPrintWriter.end();
    }
  }

  public void parseJSON(JSONObject paramJSONObject)
    throws Exception
  {
    if (paramJSONObject.has("description"))
      setDescription(paramJSONObject.getString("description"));
    if (paramJSONObject.has("semantic"))
    {
      Semantic localSemantic = new Semantic();
      localSemantic.parseJSON(paramJSONObject.getJSONObject("semantic"));
      setSemantic(localSemantic);
    }
  }

  public JSONObject createJSON()
    throws Exception
  {
    JSONObject localJSONObject = new JSONObject();
    if (StringUtils.isNotEmpty(this.description))
      localJSONObject.put("description", getDescription());
    if (this.semantic != null)
      localJSONObject.put("semantic", this.semantic.createJSON());
    return localJSONObject;
  }

  public boolean equals(Object paramObject)
  {
    return ((paramObject instanceof ADHOCTableData) && (ComparatorUtils.equals(this.description, ((ADHOCTableData)paramObject).description)) && (ComparatorUtils.equals(this.semantic, ((ADHOCTableData)paramObject).semantic)));
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    ADHOCTableData localADHOCTableData = (ADHOCTableData)super.clone();
    localADHOCTableData.setDescription(this.description);
    if (getSemantic() != null)
      localADHOCTableData.setSemantic((Semantic)getSemantic().clone());
    return localADHOCTableData;
  }
}